#!/bin/bash
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"
echo "lmdprogress:10"

function check_app_path_win {
    TARGET_SOFTWARE="LM Studio"
    PROGRAMDATA=$(powershell.exe -NoProfile -Command "Write-Output \$env:PROGRAMDATA")
    echo "PROGRAMDATA"
    echo $PROGRAMDATA
    START_MENU_PATH="$PROGRAMDATA/Microsoft/Windows/Start Menu/Programs"
    shortcut="$START_MENU_PATH/$TARGET_SOFTWARE.lnk"

    if [ -f "$shortcut" ]; then
      echo "shortcut found in the PROGRAMDATA."$shortcut
    else
      echo "shortcut not found in the PROGRAMDATA. try APPDATA dir"
      APPDATA_DIR=$(powershell.exe -NoProfile -Command "Write-Output \$env:APPDATA")
      START_MENU_PATH="$APPDATA_DIR/Microsoft/Windows/Start Menu/Programs"
      shortcut="$START_MENU_PATH/$TARGET_SOFTWARE.lnk"
      if [ -f "$shortcut" ]; then
        echo "shortcut found in the APPDATA."$shortcut
      else
        echo "shortcut not found in the APPDATA"
       
      fi
    fi

    real_path=$(powershell.exe -NoProfile -Command "[System.IO.Path]::GetFullPath((New-Object -ComObject WScript.Shell).CreateShortcut(\"$(cygpath -w "$shortcut")\").TargetPath)")
    echo "realpath: $real_path"

    INSTALL_PATH=$(dirname "$real_path")
    echo $INSTALL_PATH
}

function remove_app_win {
  echo "Find install path."
  check_app_path_win
  echo "call $INSTALL_PATH\\Uninstall LM Studio.exe"
  "$INSTALL_PATH\\Uninstall LM Studio.exe"
}

if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
    echo "macOS"
    echo "this script is for windows."
    echo "lmdprogress:100"
    exit 1
elif current_os_is_windows; then
    echo "Windows"
    remove_app_win
    echo "lmdprogress:100"
fi
